<template>
  <el-card class="eletric-param-analysis-wrapper card-header">
    <header>
      <div class="head">
        <div class="head-icon">
          <svg-icon icon-class="dashboard-4"></svg-icon>
        </div>
        <div>电气参数分析</div>
      </div>
      <div><XyTimePicker></XyTimePicker></div>
    </header>
    <div class="data-energy-container" ref="chartContainer"></div>
    <div class="analysis-wrapper electricity-consumption">
      <header>电气参数分析</header>
      <main>
        <div class="main-left">
          <div>功率数据:</div>
          <div><svg-icon icon-class="dot"></svg-icon>今日最高:3.2kW(14:30)</div>
          <div>
            <svg-icon icon-class="dot"></svg-icon>今日最低: 0.1 kW (04:00)
          </div>
          <div><svg-icon icon-class="dot"></svg-icon>今日平均: 2.85 kW</div>
          <div>
            <svg-icon icon-class="dot"></svg-icon>昨日最高: 3.0 kW (15:00)
          </div>
          <div>
            <svg-icon icon-class="dot"></svg-icon>昨日最低: 0.1 kW (03:30)
          </div>
          <div><svg-icon icon-class="dot"></svg-icon>昨日平均: 2.70 kW</div>
        </div>
        <div class="main-mid">
          <div>电流数据:</div>
          <div><svg-icon icon-class="dot"></svg-icon>今日最高:3.2kW(14:30)</div>
          <div>
            <svg-icon icon-class="dot"></svg-icon>今日最低: 0.1 kW (04:00)
          </div>
          <div><svg-icon icon-class="dot"></svg-icon>今日平均: 2.85 kW</div>
          <div>
            <svg-icon icon-class="dot"></svg-icon>昨日最高: 3.0 kW (15:00)
          </div>
          <div>
            <svg-icon icon-class="dot"></svg-icon>昨日最低: 0.1 kW (03:30)
          </div>
          <div><svg-icon icon-class="dot"></svg-icon>昨日平均: 2.70 kW</div>
        </div>
        <div class="main-right">
          <div>电压数据:</div>
          <div><svg-icon icon-class="dot"></svg-icon>今日最高:3.2kW(14:30)</div>
          <div>
            <svg-icon icon-class="dot"></svg-icon>今日最低: 0.1 kW (04:00)
          </div>
          <div><svg-icon icon-class="dot"></svg-icon>今日平均: 2.85 kW</div>
          <div>
            <svg-icon icon-class="dot"></svg-icon>昨日最高: 3.0 kW (15:00)
          </div>
          <div>
            <svg-icon icon-class="dot"></svg-icon>昨日最低: 0.1 kW (03:30)
          </div>
          <div><svg-icon icon-class="dot"></svg-icon>昨日平均: 2.70 kW</div>
        </div>
      </main>
    </div>
  </el-card>
</template>

<script>
import XyTimePicker from "@/components/XyTimePicker.vue";
import * as echarts from "echarts";

export default {
  components: {
    XyTimePicker,
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
      window.removeEventListener("resize", this.resizeChart);
    }
  },
  methods: {
    initChart() {
      this.$nextTick(() => {
        const chartDom = this.$refs.chartContainer;
        if (chartDom) {
          this.chart = echarts.init(chartDom);
          this.setChartOption();
          window.addEventListener("resize", this.resizeChart);
        }
      });
    },

    setChartOption() {
      // 生成时间标签（24小时）
      const labels = [];
      for (let i = 0; i < 24; i++) {
        labels.push(`${i}:00`);
      }

      // 生成电气参数数据
      const powerData = this.getRandomData(24, 0.1, 3.5);
      const currentData = this.getRandomData(24, 0.5, 16);
      const voltageData = this.getRandomData(24, 218, 223);

      const option = {
        color: ["#EF4444", "#8B5CF6", "#10B981"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          formatter: (params) => {
            let tooltipText = `${params[0].axisValueLabel}<br/>`;
            params.forEach((param) => {
              if (param.seriesIndex === 0) {
                tooltipText += `${param.marker} ${param.seriesName}: ${param.value} kW<br/>`;
              } else if (param.seriesIndex === 1) {
                tooltipText += `${param.marker} ${param.seriesName}: ${param.value} A<br/>`;
              } else {
                tooltipText += `${param.marker} ${param.seriesName}: ${param.value} V<br/>`;
              }
            });
            return tooltipText;
          },
        },
        legend: {
          data: ["功率", "电流", "电压"],
          top: "top",
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          top: "20%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: labels,
            axisTick: {
              alignWithLabel: true,
            },
            axisLabel: {
              color: "#9498a1",
              margin: 10,
              fontSize: 11,
            },
            axisLine: {
              lineStyle: {
                color: "rgba(148, 163, 184, 0.2)",
              },
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "功率 (kW) / 电流 (A)",
            nameTextStyle: {
              color: "#9498a1",
              padding: [0, 0, 0, 40],
              fontSize: 11,
            },
            splitLine: {
              lineStyle: {
                color: "rgba(148, 163, 184, 0.1)",
              },
            },
            axisLabel: {
              color: "#9498a1",
              fontSize: 11,
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "rgba(148, 163, 184, 0.2)",
              },
            },
          },
          {
            type: "value",
            name: "电压 (V)",
            nameTextStyle: {
              color: "#9498a1",
              padding: [0, 0, 0, 0],
              fontSize: 11,
            },
            splitLine: {
              show: false,
            },
            axisLabel: {
              color: "#9498a1",
              fontSize: 11,
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "rgba(148, 163, 184, 0.2)",
              },
            },
            min: 215,
            max: 225,
          },
        ],
        series: [
          {
            name: "功率",
            type: "line",
            data: powerData,
            yAxisIndex: 0,
            itemStyle: {
              color: "#EF4444",
            },
            lineStyle: {
              width: 2,
            },
            smooth: true,
            showSymbol: false,
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "rgba(239, 68, 68, 0.1)" },
                { offset: 1, color: "rgba(239, 68, 68, 0)" },
              ]),
            },
          },
          {
            name: "电流",
            type: "line",
            data: currentData,
            yAxisIndex: 0,
            itemStyle: {
              color: "#8B5CF6",
            },
            lineStyle: {
              width: 2,
            },
            smooth: true,
            showSymbol: false,
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "rgba(139, 92, 246, 0.1)" },
                { offset: 1, color: "rgba(139, 92, 246, 0)" },
              ]),
            },
          },
          {
            name: "电压",
            type: "line",
            data: voltageData,
            yAxisIndex: 1,
            itemStyle: {
              color: "#10B981",
            },
            lineStyle: {
              width: 2,
            },
            smooth: true,
            showSymbol: false,
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "rgba(16, 185, 129, 0.1)" },
                { offset: 1, color: "rgba(16, 185, 129, 0)" },
              ]),
            },
          },
        ],
      };

      this.chart.setOption(option);
    },

    // 生成随机数据的工具函数
    getRandomData(count, min, max) {
      return Array.from({ length: count }, () =>
        parseFloat((Math.random() * (max - min) + min).toFixed(2))
      );
    },

    resizeChart() {
      if (this.chart) {
        setTimeout(() => {
          this.chart.resize();
        }, 100);
      }
    },
  },
};
</script>

<style scoped lang="scss">
@import "./styles/common.scss";
.eletric-param-analysis-wrapper.card-header {
  .head {
    .head-icon {
      background-color: #ffedd5;
      color: #ea580c;
    }
  }
  .data-energy-container {
    width: 100%;
    height: 400px;
    margin: 15px 0;
  }
  .electricity-consumption {
    width: 100%;
    color: #9a3412;
    background-color: rgba(254, 242, 232, 0.8);
    main {
      margin-top: 10px;
      display: flex;
      .main-left,
      .main-mid,
      .main-right {
        width: 33%;
        padding: 10px;
      }
    }
  }
}
</style>
